//
//  CZEmoticonKeyBoard.swift
//  weiBo
//
//  Created by apple on 16/5/21.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class CZEmoticonKeyBoard: UIView {
    //懒加载emotiontoolbar 
    private lazy var emotionToolBar: CZEmoticonToolBar = {
        let tool = CZEmoticonToolBar(frame: CGRectZero)
        
        
        return tool
    }()
    
    //懒加载表情视图View
    private lazy var emoticonCollectionView: UICollectionView = {
        let flowlayout = UICollectionViewFlowLayout()
        let view = UICollectionView(frame: CGRectZero, collectionViewLayout: flowlayout)
        flowlayout.scrollDirection = .Horizontal
        view.pagingEnabled = true
        view.showsHorizontalScrollIndicator = false
        view.showsVerticalScrollIndicator = false
        //注册cell
        view.registerClass(CZEmoticonCollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        //去掉弹簧效果
        view.bounces = false
        view.dataSource = self
        view.delegate = self
        
        return view
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    private func setUpUI() {
        backgroundColor = UIColor.whiteColor()
        
        addSubview(emotionToolBar)
        addSubview(emoticonCollectionView)
        
        emotionToolBar.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.height.equalTo(40)
        }
        
        emoticonCollectionView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.bottom.equalTo(emotionToolBar.snp_top)
        }
        
        emotionToolBar.didSelectedEmoticonButtonClosure = { ( type: CZEmoticonToolBarButtonType) in
            let indexPath: NSIndexPath
            switch type {
            case .Normal:
                indexPath = NSIndexPath(forItem: 0, inSection: 0)
            case .Emoji:
                indexPath = NSIndexPath(forItem: 0, inSection: 1)
            case .Lxh:
                indexPath = NSIndexPath(forItem: 0, inSection: 2)
            }
            
            self.emoticonCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let flowLayOut = emoticonCollectionView.collectionViewLayout as!UICollectionViewFlowLayout
        flowLayOut.itemSize = emoticonCollectionView.frame.size
        flowLayOut.minimumInteritemSpacing = 0
        flowLayOut.minimumLineSpacing = 0
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


extension CZEmoticonKeyBoard: UICollectionViewDataSource,UICollectionViewDelegate {

//collectionView 的代理方法
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return CZEmoticonTools.sharedTools.allEmoticons.count
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return CZEmoticonTools.sharedTools.allEmoticons[section].count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CZEmoticonCollectionViewCell
        cell.indexPath = indexPath
        cell.backgroundColor = self.backgroundColor
        cell.emotions = CZEmoticonTools.sharedTools.allEmoticons[indexPath.section][indexPath.row]
        
        return cell
    }
    
    //监听滚动
    func scrollViewDidScroll(scrollView: UIScrollView) {
        //获取当前视图的cell
        let cells = emoticonCollectionView.visibleCells().sort { (firstCell, secondCell) -> Bool in
            return firstCell.x < secondCell.x
        }
        
        if cells.count == 2 {
            let firstCell = cells.first!
            let secondCell = cells.last!
            let contentOffSetX = scrollView.contentOffset.x
            //计算两个cell在屏幕中显示的大小
            let firstCellContentOffSetX = abs(firstCell.x - contentOffSetX)
            let secondCellContentOffSetX = abs(secondCell.x - contentOffSetX)
            
            let indexPath: NSIndexPath
            //获取cell的indexPath
            if firstCellContentOffSetX > secondCellContentOffSetX {
                indexPath = emoticonCollectionView.indexPathForCell(firstCell)!

            }else {
                indexPath = emoticonCollectionView.indexPathForCell(secondCell)!
            }
            
            //获取对应section
            let section = indexPath.section
            
            emotionToolBar.selectEmotionButtonWithSection(section)

        }
        
    }

}




